package com.xwy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xwy.entity.SysApply;
import com.xwy.entity.SysDepartment;
import com.xwy.service.DepartmentService;
import com.xwy.service.SysApplyService;
import com.xwy.util.TokenContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/**
 * (SysDepartment)表控制层
 *
 * @author xwy
 * @since 2020-06-11 10:42:47
 */
@Controller
public class SysDepartmentController {

    @Resource
    private DepartmentService departmentService;
    @Resource
    private SysApplyService sysApplyService;

    // 查询所有部门活动的信息
    @RequestMapping(value = "/all-department-list", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject allApplyList(@RequestParam(name = "pageNum", required = true, defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", required = true, defaultValue = "4") int pageSize, Model model) {
        JSONObject json = new JSONObject();
        String uno = TokenContext.getUno();
        List<SysDepartment> departments = departmentService.findDepartmentAll(pageNum, pageSize);
        //使用PageInfo来封装查询的数据
        PageInfo<SysDepartment> pageInfo = new PageInfo<SysDepartment>(departments);
        model.addAttribute("pageInfo", pageInfo);
        json.put("status", 0);
        json.put("message", "查询成功");
        json.put("data", pageInfo);
        return json;
    }

    // 获取单个报名信息的内容
    @RequestMapping(value = "/get-one-a", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getOneA(@RequestParam String depId) {
        JSONObject json = new JSONObject();
        SysDepartment departmentServiceOneA = departmentService.getOneA(depId);
        json.put("status", 0);
        json.put("message", "查询成功");
        json.put("data", departmentServiceOneA);
        return json;
    }

    // 学生报名活动
    @RequestMapping(value = "/user/regist-apply", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject usersApply(@RequestBody JSONObject object) throws ParseException {
        String data = object.toJSONString();
        //解析json数据
        JSONObject json = JSON.parseObject(data);
        String depId = json.getString("depId");
        System.out.println(depId);
        String cause = json.getString("cause");
        String uno = TokenContext.getUno();
        SysApply apply = sysApplyService.userApply(depId, uno, cause);
        if (apply != null) {
            SysApply sysApply = sysApplyService.findByAid(apply.getAid(), uno);
            sysApply.setApplyStatus(1);
            boolean b = sysApplyService.updateStatus(sysApply.getApplyStatus(), sysApply.getAid());
            json.put("status", 1);
            json.put("message", "未审核");
        } else {
            json.put("status", 0);
            json.put("message", "提交失败");
        }
        return json;
    }

    // 查看我的报名纪录
    @RequestMapping(value = "/find-apply", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject findApply(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize) {
        JSONObject json = new JSONObject();
        String uno = TokenContext.getUno();
        List<SysApply> applyServiceByUno = sysApplyService.findByUno(uno, pageNum, pageSize);
        //使用PageInfo来封装查询的数据
        PageInfo<SysApply> pageInfo = new PageInfo<SysApply>(applyServiceByUno);
//        model.addAttribute("pageInfo", pageInfo);
        json.put("status", 0);
        json.put("message", "查询成功");
        json.put("data", pageInfo);
        return json;
    }

    //取消报名
    @RequestMapping(value = "/user/del-apply", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject cancelActivity(@RequestParam String aid) {
        JSONObject json = new JSONObject();
        boolean deleteByAid = sysApplyService.deleteByAid(aid);
        if (deleteByAid) {
            json.put("status", 0);
            json.put("message", "取消报名成功");
        } else {
            json.put("status", 1);
            json.put("message", "取消报名失败");
        }
        return json;
    }

}
